package net.yunxinyong.cec.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import net.yunxinyong.cec.entity.EcnomicBrances;
import net.yunxinyong.cec.entity.EcnomicInventory;
import net.yunxinyong.cec.entity.EcnomicInventoryExample;
import net.yunxinyong.cec.mapper.EcnomicInventoryMapper;
import net.yunxinyong.cec.service.InventoryManagerService;
import net.yunxinyong.cec.utils.ListUtils;
import net.yunxinyong.cec.utils.PageBean;
import net.yunxinyong.cec.vo.EcnomicInventoryResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 李跃
 * @date 18/9/20
 */
@Service
public class InventoryManagerServiceImpl implements InventoryManagerService {
    @Autowired
    private EcnomicInventoryMapper ecnomicInventoryMapper;
    @Override
    public PageBean listEnterpriseByName(String name, String street, int page, int rows) {
        if(page != 0 && rows != 0){
            PageHelper.startPage(page,rows);
        }
        EcnomicInventoryExample ecnomicInventoryExample = new EcnomicInventoryExample();
        EcnomicInventoryExample.Criteria criteria = ecnomicInventoryExample.createCriteria();
        //把英文街道简称转化为数据库中的街道的中文名字
        String streetName = formatStreet(street);
        //对企业的名字做判断,如果企业的名字是 空字符串"",则查询所有
        if(name.equals("")){
            criteria.andReferenceAddress02EqualTo(streetName);
        }else {
            criteria.andUnitDetailedNameLike("%"+name+"%").andReferenceAddress02EqualTo(streetName);
        }

        List<EcnomicInventory> ecnomicInventoryList = ecnomicInventoryMapper.selectByExample(ecnomicInventoryExample);
        List<EcnomicInventoryResult> ecnomicInventoryResultlist = Lists.newArrayList();
        dateFormat(ecnomicInventoryList, ecnomicInventoryResultlist);

        PageBean<EcnomicInventoryResult> pageBean = new PageBean<>();
        PageInfo<EcnomicInventory> pageInfo = new PageInfo<>(ecnomicInventoryList);
        pageBean.setTotal(pageInfo.getTotal());
        pageBean.setTotalPages(pageInfo.getPages());
        pageBean.setPageDatas(ecnomicInventoryResultlist);
        return pageBean;
    }

    @Override
    public Map<String, Integer> getNumberOfInventoryStatus(String street) {
        //把街道的简称转化成数据库中街道的名字
        String dataStreet = formatStreet(street);
        //查询企业的总数量
        EcnomicInventoryExample ecnomicInventoryExample = new EcnomicInventoryExample();
        ecnomicInventoryExample.createCriteria().andReferenceAddress02EqualTo(dataStreet);
        int totalEnterprise = ecnomicInventoryMapper.countByExample(ecnomicInventoryExample);
        //查询已清查企业的数量
        EcnomicInventoryExample ecnomicInventoryExample1 = new EcnomicInventoryExample();
        ecnomicInventoryExample1.createCriteria().andReferenceAddress02EqualTo(dataStreet).andUiteStatusEqualTo(1);
        int inventoryEnterprise = ecnomicInventoryMapper.countByExample(ecnomicInventoryExample1);
        //查询未清查企业的数量
        EcnomicInventoryExample ecnomicInventoryExample2 = new EcnomicInventoryExample();
        ecnomicInventoryExample2.createCriteria().andReferenceAddress02EqualTo(dataStreet).andUiteStatusEqualTo(2);
        int unInventoryEnterprise = ecnomicInventoryMapper.countByExample(ecnomicInventoryExample2);

        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalEnterprise",totalEnterprise);
        map.put("inventoryEnterprise",inventoryEnterprise);
        map.put("unInventoryEnterprise",unInventoryEnterprise);
        return map;
    }


    /**
     * 这个方法是 把查询到的企业 ecnomicInventoryList放入ecnomicInventoryResultlist中并设置填表日期的显示格为 yyyy-MM-dd
     * @param ecnomicInventoryList
     * @param ecnomicInventoryResultlist
     */
    private void dateFormat(List<EcnomicInventory> ecnomicInventoryList, List<EcnomicInventoryResult> ecnomicInventoryResultlist) {
        for (EcnomicInventory ecnomicInventory:ecnomicInventoryList){
            EcnomicInventoryResult ecnomicInventoryResult = new EcnomicInventoryResult();
            BeanUtils.copyProperties(ecnomicInventory,ecnomicInventoryResult);
            ecnomicInventoryResult.setValue(ecnomicInventory.getUnitDetailedName());
            //修改企业清查状态
            switch (ecnomicInventory.getUiteStatus()){
                case 1: ecnomicInventoryResult.setStatusString("已清查");
                    break;
                case 2: ecnomicInventoryResult.setStatusString("未清查");
                    break;
                case 3: ecnomicInventoryResult.setStatusString("清查中");
                    break;
                case 4: ecnomicInventoryResult.setStatusString("企业未找到");
                    break;
                case 5: ecnomicInventoryResult.setStatusString("企业已注销");
                    break;

                default:break;
            }
            //添加单位营业状态的文字描述
            //因为营业状态从 int改成了 string 类型 所以下面这段 代码暂时不用
            /*if(ecnomicInventory.getBusinessState() != null){
                switch (ecnomicInventory.getBusinessState()){
                    case 1 : ecnomicInventoryResult.setBusinessStateString("营业");
                        break;
                    case 2 : ecnomicInventoryResult.setBusinessStateString("停业(歇业)");
                        break;
                    case 3 : ecnomicInventoryResult.setBusinessStateString("筹建");
                        break;
                    case 4 : ecnomicInventoryResult.setBusinessStateString("当年关系");
                        break;
                    case 5 : ecnomicInventoryResult.setBusinessStateString("当年破产");
                        break;
                    case 6 : ecnomicInventoryResult.setBusinessStateString("当年注销");
                        break;
                    case 7 : ecnomicInventoryResult.setBusinessStateString("当年吊销");
                        break;
                    case 9 : ecnomicInventoryResult.setBusinessStateString("其他");
                        break;
                    default:break;
                }
            }*/

            /**
             * 添加分支列表
             */
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            //对填表日期进行判断,如果formTime为null则把日期设置为 " "
            if(ecnomicInventory.getFormTime() != null ){
                String formTime = format.format(ecnomicInventory.getFormTime());
                ecnomicInventoryResult.setFormTime(formTime);
            }else {
                ecnomicInventoryResult.setFormTime(" ");
            }
            ecnomicInventoryResultlist.add(ecnomicInventoryResult);
        }


    }

    /**
     * 这个方法 英文的街道简称,转化成中文的街道名称,然后去数据库查询数据
     *  如果没有匹配的选项,怎返回空字符串
     * @param street
     * @return
     */
    private String formatStreet(String street){
        switch (street){
            case "cyjxq":
                street="北京市朝阳区酒仙桥街道";
                return street;
            case "mtgdy":
                street="北京市门头沟区大峪街道";
                return street;
            case "mtgsl":
                street="北京市门头沟区石龙";
                return street;
            case "cymzd":
                street="北京市朝阳区麦子店街道";
                return street;
            case "mtgyd":
                street="北京市门头沟区永定街道";
                return street;
            case "hdzgc":
                street="北京市海淀区中关村街道";
                return street;
            case "hdbxg":
                street="北京市海淀区北下关街道";
                return street;
            case "cyjs":
                street="北京市朝阳区劲松街道";
                return street;
            default:break;
        }
        return "";
    }


}
